<?php

/**
 * @package    Delcampe_Translate
 *
 * @copyright Sebastien Delcampe
 * @license Please, don't read or copy
 * @author afernando
 *
 * @version 1.0
 **/
class Lion_View_Helper_Translate extends Zend_Controller_Action_Helper_Abstract
{
  public function translate ($label)
  {
    $sess = new Zend_Session_Namespace('locale');
	$curr_lang = $sess->current_lang;
	$registry = Zend_Registry::getInstance();
    $this->db = $registry->get('db');
    //$locale = new Zend_Locale('browser');
    //$locale = substr($locale, 0, 2);
    $locale = $curr_lang;
    if($curr_lang != '')
    {
    	$lang = $curr_lang;
    } else {
    	$lang = 'en';
    }
    $string = null;
    
    //$translator = Zend_Registry::get('translate');
    
    //$string = $translator->_($label); 
    
    $sql = $this->db->fetchAll("
    	SELECT id 
    	FROM td_labels
    	WHERE language = '" . $lang . "' AND
    	contents = '" . $label ."' ");
    
    if(count($sql) == 0)
    {
    	//$curr_lang = 'en';
    	$add_labels = $this->db->query("
		      INSERT
		      INTO td_labels_key(keyText)
		      VALUES ('" . $label . "')");
        $last_insert_id = $this->db->lastInsertId();
        if ($add_labels) {
        	$add_labels_key = $this->db->query("
		        INSERT
		        INTO td_labels
		        VALUES(" . $last_insert_id . ", '" . $lang . "', '" . $label . "', '', now())");
		        
  		$add_version = $this->db->query("
	        INSERT 
	        INTO td_labels_version
			VALUES(" . $last_insert_id . ", '" . $lang . "', '1', '1', '" . $label . "', '', now())");
        }
    }
    
    $string = str_replace('%', '', $label);   	
    	
    return str_replace('<p>', '', $label);
  }
}
